package com.young.monitor.web;

import com.fasterxml.jackson.core.type.TypeReference;
import com.young.common.util.JsonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.model.ApiMonitorDTO;
import com.young.interfaces.log.model.LogDTO;
import com.young.monitor.service.IApiIndexService;
import com.young.monitor.service.IApiTraceService;
import com.young.monitor.service.ILogCollectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 日志收集
 *
 * @author imrookie
 * @date 2019/5/19
 */
@Controller
@RequestMapping(value = "/monitor/collect", method = {RequestMethod.GET, RequestMethod.POST})
public class LogCollectController {

    @Resource(name = "logCollectService")
    ILogCollectService logCollectService;

    /**
     * 接收监控日志
     * @param log
     * @return
     */
    @RequestMapping("/receiveLog")
    @ResponseBody
    public Object receiveLog(String log) {
        if (StringUtils.isBlank(log)) {
            return Result.buildFail("日志信息为空");
        }
        logCollectService.handleLogDTO(JsonUtil.toBean(log, LogDTO.class));
        return Result.buildSuccess();
    }

    /**
     * 批量接收日志
     * @param logs
     * @return
     */
    @RequestMapping("/batchReceiveLog")
    @ResponseBody
    public Object batchReceiveLog(String logs) {
        if (StringUtils.isBlank(logs)) {
            return Result.buildFail("日志信息为空");
        }
        logCollectService.handleLogDTO(JsonUtil.toBean(logs, new TypeReference<List<LogDTO>>() {}));
        return Result.buildSuccess();
    }

    /**
     * 批量收集接口监控信息
     * @param monitors
     * @return
     */
    @RequestMapping("/batchReceiveMonitor")
    @ResponseBody
    public Object batchReceiveMonitor(String monitors) {
        if (StringUtils.isBlank(monitors)) {
            return Result.buildFail("接口监控信息为空");
        }
        logCollectService.handleApiMonitorDTO(JsonUtil.toBean(monitors, new TypeReference<List<ApiMonitorDTO>>() {}));
        return Result.buildSuccess();
    }
}
